import axios from "axios";
import {STORE} from "@/scripts/account";

export const TransportStatus = {
    'CREATED': '运单创建',
    'DEPART': '司机接单',
    'ARRIVE': '到达仓库',
}

export const TransportStatusFilters = [
    {text: '运单创建', value: '运单创建'},
    {text: '司机接单', value: '司机接单'},
    {text: '到达仓库', value: '到达仓库'},
]

export const chooseTagType = (txt) => {
    if (txt == TransportStatus['CREATED']) {
        return 'warning'
    } else if (txt == TransportStatus['DEPART']) {
        return 'success'
    } else {
        return 'info'
    }
}

export const getTransportList = (callback) => {
    axios.get('/transport').then((response) => {
        let rs = response.data
        for (let i = 0; i < rs.length; i++) {
            rs[i].status = TransportStatus[rs[i].status]
        }
        callback(rs);
    });
};

export const getTransportListByDriver = (callback) => {
    axios.get('/transport/driver/' + STORE.userState.data.username).then((response) => {
        let rs = response.data
        for (let i = 0; i < rs.length; i++) {
            rs[i].status = TransportStatus[rs[i].status]
        }
        callback(rs);
    })
}

function getDriverIdByUsername(callback) {
    axios.get('/driver/getIdbyUsername/' + STORE.userState.data.username).then((response) => {
        callback(response.data)
    })
}

export const svDriverDepart = (callback) => {
    getDriverIdByUsername((result) => {
        axios.post('/driver/begin/' + result).then((response) => {
            callback(response.data)
        })
    })
}

export const svDriverArrive = (callback) => {
    getDriverIdByUsername((result) => {
        axios.post('/driver/arrive/' + result).then((response) => {
            callback(response.data)
        })
    })
}

export const getTransport = (id, callback) => {
    axios.get('/transport/' + id).then((response) => {
        callback(response.data)
    })
}

export const deleteTransport = (id, callback) => {
    axios.delete('/transport/' + id).then((response) => {
        callback(response.data);
    });
}

export const updateTransport = (transportData, callback) => {
    axios.put('/transport/' + transportData.tnum, transportData).then((response) => {
        callback(response.data ? 1 : 0);
    })
}

export const createTransport = (transportData, callback) => {
    axios.post('/transport', transportData).then((response) => {
        callback(response.data ? 1 : 0)
    })
}